// pages/cardTicket/cardTicket.js
const app = getApp();
import couponApi from '../../service/couponApi'
import activityApi from '../../service/activityApi'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        cardList: [],
        showRegimentRules: false, // 拼团规则弹窗
        couponType: app.globalData.codeConfig.couponType,
        pages: {
            startPage: 1,
            pageSize: 9999
        },
        showEmpty: false,
        showLoadingView: false,
        openVoteFlag: false,
        orderInfo: {}, // 订单信息
        groupId: '', // 拼团ID
        userCouponId: '', // 用户选中优惠券ID
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            groupId: wx.getStorageSync('groupId')
        })
        this.setData({
                orderInfo: app.globalData.orderInfo,
                userCouponId: app.globalData.couponInfo.userCouponId || ''
            })
            // this.getVoteOpenInfo()
        this.getCouponList()
    },

    // 获取投票开启信息
    // getVoteOpenInfo () {
    //   let that = this
    //   activityApi.getVoteOpenInfo().then((res) => {
    //     if (app.globalData.codeConfig.errCode.SUCCESS_CODE === res.data.errCode) {
    //       that.setData({
    //         openVoteFlag: res.data.data.openFlag
    //       })
    //     }
    //   })
    // },

    // 获取卡券列表
    getCouponList() {
        let that = this
        if (that.data.cardList.length === 0) {
            that.setData({
                showLoadingView: true
            })
        }
        // wx.showLoading({
        //   title: "加载中..",
        //   mask: true
        // })
        let params = {
            goodsId: that.data.orderInfo.goodsId,
            quantity: that.data.orderInfo.quantity,
            groupId: that.data.groupId,
            ...that.data.pages
        }
        couponApi.coupon(params).then(res => {
            that.setData({
                showLoadingView: false
            })
            wx.stopPullDownRefresh(); // 阻止下拉刷新
            if (res.data.errCode === app.globalData.codeConfig.errCode.SUCCESS_CODE) {
                // wx.hideLoading() // 隐藏数据加载
                var list = res.data.data.couponList
                if (list.length === 0) {
                    that.setData({
                        showEmpty: true
                    })
                } else {
                    that.setData({
                        showEmpty: false
                    })
                }
                var _index
                if (that.data.userCouponId !== '') {
                    for (let i = 0; i < list.length; i++) {
                        if (that.data.userCouponId === list[i].userCouponId) {
                            list[i].selected = true
                            _index = i
                            break;
                        }
                    }
                }
                // list[_index].selected = true
                // list.map(i => {
                //   if (that.data.userCouponId === i.userCouponId) {
                //     i.selected = true
                //   }
                // })
                that.setData({
                    cardList: list
                })
            } else {
                that.setData({
                        showLoadingView: false
                    })
                    // wx.hideLoading() // 隐藏数据加载
                wx.showToast({
                    title: res.data.msg,
                    icon: 'none'
                })
            }
        }).catch(e => {
            wx.stopPullDownRefresh(); // 阻止下拉刷新
            wx.hideLoading() // 隐藏数据加载
        })
    },

    // 选择优惠券
    onSelCoupon(e) {
        var item = e.currentTarget.dataset.item
        app.globalData.couponInfo = item
        app.globalData.couponInfo.dontChoose = false
        app.globalData.isSelectCoupon = true
        wx.navigateBack({
            delta: 1
        })
    },
    // 不使用优惠券按钮
    dontUseCoupon() {
        app.globalData.couponInfo = {
            dontChoose: true,
            discountPrice: '0'
        }
        app.globalData.isSelectCoupon = true
        wx.navigateBack({
            delta: 1
        })
    },
    // 参与投票，获得更多优惠券按钮
    goVote(e) {
        let url = e.target.dataset.url
        wx.navigateTo({
            url
        })
    },
    // 显示拼团规则弹窗 
    onShowRegimentRules() {
        this.setData({
            showRegimentRules: true
        })
    },
    // 关闭拼团规则弹窗
    closeRegimentRules() {
        this.setData({
            showRegimentRules: false
        })
    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        this.getCouponList()
    }
})